<?php

namespace common\service;

use common\exception\LogicException;
use common\model\EntityModel;
use common\model\TaskModel;
use support\ErrorCode;
use support\SingletonTrait;
use think\Exception;

class ShotEnableService
{

    use SingletonTrait;

    /**
     * 恢复清单状态
     * @param $entityList
     * @return bool
     * @throws Exception
     */
    public function updateEntityJson($entityList)
    {
        $updData = [];
        if (empty($entityList)) {
            throw new LogicException('entity data is null', ErrorCode::GET_ENTITY_DATA_FAIL);
        }
        $progressStatus = StatusService::getInstance()->getTaskDefaultStartStatus();
        foreach ($entityList as $val) {
            $jsonOld = $val['json'];
            $prefix = module_by_id($val['module_id'])['code'] ?? '';
            $field = $prefix . '_deprecate_data';
            if (empty($jsonOld[$field])) {
                $json = $jsonOld;
                $json[$prefix . '_status'] = $progressStatus['id'];
            } else {
                $json = $jsonOld[$field];
            }
            $json[$field] = '';
            $updData[] = [
                'id' => $val['id'],
                'json' => $json,
            ];
        }
        if (!empty($updData)) {
            CommonService::instance('entity')->commonBatchUpdate($updData);
        }
        return true;
    }

}